package com.readfeed.app.adapter;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.readfeed.app.R;
import com.readfeed.app.model.Category;

public class CategoriesListAdapter extends ArrayAdapter<Category> {
	
	private List<Category> mDisplayCategories;
	private List<Category> mSelectedCategories;
	private SparseBooleanArray mSelection;
	private Activity mContext;
	
	public CategoriesListAdapter(Activity context, int resourceId, List<Category> categories) {
		super(context, resourceId, categories);
		this.mDisplayCategories = categories;		
		this.mContext = context;
		this.mSelection = new SparseBooleanArray();
		this.mSelectedCategories = new ArrayList<Category>();
	}	
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder;
		
		if (convertView == null) {
			LayoutInflater inflater = mContext.getLayoutInflater();
			convertView = inflater.inflate(R.layout.category_row, null);
			
			viewHolder = new ViewHolder();
			viewHolder.title = (TextView) convertView.findViewById(R.id.category_title);
			viewHolder.channelsCount = (TextView) convertView.findViewById(R.id.channels_count_text);
			viewHolder.rightArrow = (ImageView) convertView.findViewById(R.id.category_arrow_icon);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder)convertView.getTag();
		}
		
		Category category = this.mDisplayCategories.get(position);				
		if (category != null) {
			viewHolder.title.setText(category.getTitle());
			viewHolder.channelsCount.setText("Channels:(" + category.getChannels().size() + ")");
		}
		
		if (mSelection.get(position)) {
			viewHolder.rightArrow.setImageResource(R.drawable.ic_check);
		} else {
			viewHolder.rightArrow.setImageResource(R.drawable.ic_right_arrow);
		}
		
		return convertView;		
	}
	
	public void addCategory(Category category){
		if (category != null) {
			this.mDisplayCategories.add(category);
			notifyDataSetChanged();					
		}		
	}
	
	public void removeCategory(int pos){
		this.mDisplayCategories.remove(pos);
		notifyDataSetChanged();
	}
	
	public Category getCategoryByPosition(int position){
		return mDisplayCategories.get(position);
	}
	
	public int categoriesCount(){
		return this.mDisplayCategories.size();
	}
	
	public List<Category> getSelectedCategories(){
		return mSelectedCategories;
	}
	
	public void removeSelectedCategories(){
		for (int i = 0; i < mSelectedCategories.size(); i++) {
			Category categoryToRemove = mSelectedCategories.get(i);
			mDisplayCategories.remove(categoryToRemove);
		}
		
		notifyDataSetChanged();
	}
	
	public void setNewSelection(int position, boolean value){
		this.mSelection.put(position, value);
		Category cat = mDisplayCategories.get(position);
		mSelectedCategories.add(cat);
		notifyDataSetChanged();
	}
	
	public boolean isPositionChecked(int position){
		Boolean result = this.mSelection.get(position);
		if (result == null) {
			return false;
		}
		
		return result;
	}
	
	public void removeSelection(int position){
		this.mSelection.delete(position);
		Category cat = mDisplayCategories.get(position);
		mSelectedCategories.remove(cat);
		notifyDataSetChanged();
	}
	
	public void clearSelection(){
		this.mSelection = new SparseBooleanArray();
		mSelectedCategories = new ArrayList<Category>();
		notifyDataSetChanged();
	}
	
	private static class ViewHolder{
		TextView title;
		TextView channelsCount;
		ImageView rightArrow;
	}
}
